/**
 * 
 */
package pyrachnid.view.component;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Locale;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileSystemView;

/**
 * @author zygou
 *
 */
public class MapPreview extends JPanel implements PropertyChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private MapFileChooser chooser;
	private JLabel preview, nom, taille, resolution;
	
	public MapPreview(MapFileChooser chooser) {
		super(new GridBagLayout());	
		
		setPreferredSize(new Dimension(300, 200));
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth=2;
		
		add(preview = new JLabel(),c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth=2;
		add(nom = new JLabel(""),c);
		
		c.gridx = 0;
		c.gridy = 2;
		add(taille = new JLabel(""),c);
		
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth=2;
		add(resolution = new JLabel(""),c);
		
		this.chooser = chooser;
		this.chooser.addPropertyChangeListener(this);
		setBorder(new TitledBorder("Preview"));
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();
		
		if(MapFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)){
			
			File file = (File) e.getNewValue();
			if(file == null){
				clear();
				return;	
			}
			
			FileSystemView vueSysteme = FileSystemView.getFileSystemView(); 
			Locale locale = Locale.getDefault();
			NumberFormat nf = NumberFormat.getInstance(locale);
			
			try {
				
				BufferedImage bi = ImageIO.read(file);
				int resX = bi.getWidth();
				int resY = bi.getHeight();
				double scaleValue;
				scaleValue = (resX > resY ? 200.0/resX : 200.0/resY);
				
				AffineTransform tx = new AffineTransform();
		        tx.scale(scaleValue, scaleValue);
		        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
		        BufferedImage biNew = new BufferedImage( (int) (resX * scaleValue), (int) (resY * scaleValue),
		                bi.getType());
		        bi = op.filter(bi, biNew);
				
				preview.setIcon(new ImageIcon(bi));
				nom.setText("nom : " + vueSysteme.getSystemDisplayName(file));	
				String tailleFile = "taille : "+nf.format(file.length()/1024.0)+" Ko";
				taille.setText(tailleFile);
				resolution.setText("résolution : " + resX+"x"+resY);
			} 
			catch (IOException e1) {
				clear();
			}			
		}
		else{
			clear();
		}
	}
	
	private void clear(){	
		setSize(300, 500);
		preview.setIcon(null);
		nom.setText("");	
		taille.setText("");
	}
}
